package com.copera.cn.kaka;

import org.json.JSONObject;

import android.content.pm.ActivityInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.copera.cn.kaka.util.ActionUrls;
import com.copera.cn.kaka.util.ActivityManager;
import com.copera.cn.kaka.util.HttpUtils;
import com.copera.cn.kaka.util.JsonUtil;
import com.copera.cn.kaka.vo.PointPraiseSetVo;

/**
 * 点赞启用
 * 
 * @author Administrator
 *
 */
public class PointPraiseStartActivity extends BaseActivity {
	private RelativeLayout back, relative_03, relative_05;
	private ImageView enabled, close;
	private String type;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActivityManager.getInstance().push(this);
		setContentView(R.layout.point_praise_start);
		back = (RelativeLayout) findViewById(R.id.back);
		relative_03 = (RelativeLayout) findViewById(R.id.relative_03);
		relative_05 = (RelativeLayout) findViewById(R.id.relative_05);
		enabled = (ImageView) findViewById(R.id.start);
		close = (ImageView) findViewById(R.id.close);
		if(getAppCustomer().getSetVo()!=null){
			if(getAppCustomer().getSetVo().getEnabled()!=null&&getAppCustomer().getSetVo().getEnabled().equals("开放")){
				relative_03.setBackgroundResource(R.color.enabled);
				relative_05.setBackgroundResource(R.color.close);
				enabled.setVisibility(View.VISIBLE);
				close.setVisibility(View.GONE);
			}else{
				relative_05.setBackgroundResource(R.color.enabled);
				relative_03.setBackgroundResource(R.color.close);
				enabled.setVisibility(View.GONE);
				close.setVisibility(View.VISIBLE);
			}
		}
		back.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();

			}
		});
		relative_03.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				relative_03.setBackgroundResource(R.color.enabled);
				relative_05.setBackgroundResource(R.color.close);
				enabled.setVisibility(View.VISIBLE);
				close.setVisibility(View.GONE);
				type="1";
				enabled();

			}
		});
		relative_05.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				relative_05.setBackgroundResource(R.color.enabled);
				relative_03.setBackgroundResource(R.color.close);
				enabled.setVisibility(View.GONE);
				close.setVisibility(View.VISIBLE);
				type="2";
				enabled();

			}
		});
		
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {

				case ActionUrls.FAILURE:
					Toast.makeText(PointPraiseStartActivity.this, (String) msg.obj,Toast.LENGTH_SHORT).show();
					break;
				}
			}
		};
		//强制为竖屏 
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

	}

	// 开放
	private void enabled() {

		new AsyncTask<String, Integer, JSONObject>() {

			@Override
			protected JSONObject doInBackground(String... params) {
				JSONObject jsonObj = null;
				try {
					String jsonStr = HttpUtils.get(params[0], getAppCustomer()
							.getHttpClient());

					if (jsonStr != null) {
						jsonObj = new JSONObject(jsonStr);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return jsonObj;
			}

			@Override
			protected void onPostExecute(JSONObject result) {

				if (result != null) {
					try {

						PointPraiseSetVo vo = JsonUtil.toPointPraiseSetVo(result);
						getAppCustomer().setSetVo(vo);
						Message msg = null;
						if ("开放".equals(vo.getEnabled())) {
							msg = handler.obtainMessage(ActionUrls.FAILURE, "点赞启用成功");
						}else {
							msg = handler.obtainMessage(ActionUrls.FAILURE, "点赞关闭成功");
						}
						handler.sendMessage(msg);
						finish();

					} catch (Exception e) {
						e.printStackTrace();
						if (e.getMessage() != null) {

						}
					}
				}
			}

		}.execute(ActionUrls.URL_POINT_PRAISE_ENABLE + "?shopId="
				+ getAppCustomer().getShop().getShopId() + "&type="+type);
	}

	
}
